package org.apache.http.a;

import java.io.Serializable;
import java.nio.CharBuffer;
import org.apache.http.annotation.NotThreadSafe;

@NotThreadSafe
public final class d implements Serializable, CharSequence {
    private char[] a;
    private int b;

    public d(int i) {
        a.a(i, "Buffer capacity");
        this.a = new char[i];
    }

    private void b(int i) {
        Object obj = new char[Math.max(this.a.length << 1, i)];
        System.arraycopy(this.a, 0, obj, 0, this.b);
        this.a = obj;
    }

    public void a() {
        this.b = 0;
    }

    public void a(char c) {
        int i = this.b + 1;
        if (i > this.a.length) {
            b(i);
        }
        this.a[this.b] = c;
        this.b = i;
    }

    public void a(int i) {
        if (i > 0 && i > this.a.length - this.b) {
            b(this.b + i);
        }
    }

    public void a(String str) {
        if (str == null) {
            str = "null";
        }
        int length = str.length();
        int i = this.b + length;
        if (i > this.a.length) {
            b(i);
        }
        str.getChars(0, length, this.a, this.b);
        this.b = i;
    }

    public void a(char[] cArr, int i, int i2) {
        if (cArr != null) {
            if (i < 0 || i > cArr.length || i2 < 0 || i + i2 < 0 || i + i2 > cArr.length) {
                throw new IndexOutOfBoundsException("off: " + i + " len: " + i2 + " b.length: " + cArr.length);
            } else if (i2 != 0) {
                int i3 = this.b + i2;
                if (i3 > this.a.length) {
                    b(i3);
                }
                System.arraycopy(cArr, i, this.a, this.b, i2);
                this.b = i3;
            }
        }
    }

    public char charAt(int i) {
        return this.a[i];
    }

    public int length() {
        return this.b;
    }

    public CharSequence subSequence(int i, int i2) {
        if (i < 0) {
            throw new IndexOutOfBoundsException("Negative beginIndex: " + i);
        } else if (i2 > this.b) {
            throw new IndexOutOfBoundsException("endIndex: " + i2 + " > length: " + this.b);
        } else if (i <= i2) {
            return CharBuffer.wrap(this.a, i, i2);
        } else {
            throw new IndexOutOfBoundsException("beginIndex: " + i + " > endIndex: " + i2);
        }
    }

    public String toString() {
        return new String(this.a, 0, this.b);
    }
}
